﻿using System;
using System.Data;
using System.Web.UI;
using MyWeb.Business;
using MyWeb.Common;
using MyWeb.Data;

namespace MyWeb.Admin.Usercontrols
{
    public partial class ucCompanyView : UserControl
    {
        private readonly CompanyService _companyService = new CompanyService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = _companyService.Company_GetByAll();
                if (dt.Rows.Count > 0)
                {
                    hfId.Value = dt.Rows[0]["id"].ToString();
                    txtName.Text = dt.Rows[0]["name"].ToString();
                    txtAddress.Text = dt.Rows[0]["address"].ToString();
                    txtPhone.Text = dt.Rows[0]["phone"].ToString();
                    txtEmail.Text = dt.Rows[0]["email"].ToString();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var company = new CompanyInfo
                              {
                                  Name = txtName.Text,
                                  Address = txtAddress.Text,
                                  Phone = txtPhone.Text,
                                  Email = txtEmail.Text
                              };
            DataTable dt = _companyService.Company_GetByAll();
            if (dt.Rows.Count > 0)
            {
                company.Id=Int32.Parse(hfId.Value);
                _companyService.Company_Update(company);
                WebMsgBox.Show("Cập nhật thành công!");
            }
            else
            {
                _companyService.Company_Insert(company);
                WebMsgBox.Show("Cập nhật thành công!");
            }
        }
    }
}